<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;
use think\facade\View;
use think\exception\ValidateException;

// 培训历史导入
class Tna extends BaseController
{
    public function index()
    {
        return View::fetch();
    }




    public function load(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }

        $where = [];
        $rows = Db::name('tna')
            ->where($where)
            ->order('add_time', 'desc')
            ->select()
            ->toArray();

        foreach ($rows as &$v) {
            $account = Db::name('admin_user')->where('id', '=', $v['uid'])->field('givenname, surname')->find();
            $v['admin_user'] = $account['givenname'].' '.$account['surname'];
            if ($v['flex_id'] == 0) {
                $v['flex'] = '培训历史';
            } else {
                $v['flex'] = get_info($v['flex_id'], 'flex', 'title');
            }
        }
        $data = array(
            'code'=> 0,
            'msg'=> '',
            'count' => 0,
            'data'=> $rows
        );
        return json($data);
    }

    public function import()
    {
        return View::fetch();
    }



    public function executeImport(Request $request)
    {
        if (!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return $this->outputAjax('无操作权限', 2);
        }

        set_time_limit(0);

        $filename = $request->param('path');

        $name = $request->param('name');

        try {

            // 获取excel表格数据
            $result = get_excel_info($filename);
            if ($result['code'] == 1) {
                return $this->outputAjax($result['msg'], 2);
            }

            $tid = Db::name('tna')
                ->insertGetId(['uid'=>session('admin_auth.id'),'flex_id'=>0,'title'=>$name,'path'=>$filename,'add_time'=>time(),'update_time'=>time()]);

            foreach ($result['data'] as $key => $value) {
                $userid = $value[0];
                $courseid = $value[1];
                $course_title = $value[2];
                $type = $value[3];
                $year = $value[4];


                $t = [];
                $t['flex_id'] = 0;
                $t['status'] = 1;
                $t['coursetitle'] = $course_title;
                $t['courseid'] = $courseid;
                $t['userid'] = $userid;
                $t['type'] = strtolower($type);
                $ok = Db::name('tna_data')->where($t)->find();
                $t['tid'] = $tid;
                $t['add_time'] = time();
                $t['date'] = $year;
                if ($ok) {
                    $t['status'] = 0;
                }else{
                    $t['status'] = 1;
                }

                Db::name('tna_data')->insert($t);
            }
            $a = intval(Db::name('tna_data')->where(['tid'=>$tid,'status'=>1])->count());
            $b = intval(Db::name('tna_data')->where(['tid'=>$tid,'status'=>0])->count());
            Db::name('tna')->where('id', '=', $tid)->update(['ok'=>$a,'fail'=>$b]);
            return $this->outputAjax('导入成功！', 0, [$a, $b]);
        } catch (\Exception $e) {
            return $this->outputAjax($e->getMessage(), 2);
        }
    }
}